//
//  UIScrollView+HXSPullRefresh.m
//  store
//
//  Created by J006 on 16/6/3.
//  Copyright © 2016年 huanxiao. All rights reserved.
//

#import "UIScrollView+HXSPullRefresh.h"

@interface UIScrollView()

@property (nonatomic, weak) HXSPullRefreshImageView *topShowView;

@end

@implementation UIScrollView (HXSPullRefresh)

static char HXSRefreshHeaderViewKey;

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)initTheProgressImageView {
    if (self.topShowView) {
        return;
    }
    HXSPullRefreshImageView * headerView = nil;
    headerView = [[HXSPullRefreshImageView alloc] initWithFrame: CGRectMake(0, -pullBallHeight, [self screenWidth], pullTopViewHeight)];
    [headerView setContentMode: UIViewContentModeScaleAspectFit];
    NSBundle * bundle = [NSBundle mainBundle];
    NSString * bundlePath = [bundle pathForResource: @"SVTableView"ofType: @"bundle"];
    if (bundlePath) {
        bundle = [NSBundle bundleWithPath: bundlePath];
    }
    NSString * gifPathStr = [bundle pathForResource: @"59"ofType: @"gif"];
    NSURL * url = [[NSURL alloc] initFileURLWithPath: gifPathStr];
    [headerView yh_setImage: url];
    headerView.topRefreshStatus = HXSPullRefreshHeaderStateNone;
    headerView.parentScrollView = self;
    [self addSubview: headerView];
    self.topShowView = headerView;
}

// MARK: - Public Methods

- (void)addRefreshHeaderWithCallback: (void( ^ )())callback {
    [self initTheProgressImageView];
    self.topShowView.refreshingCallback = callback;
    self.loadingImageView = [self createTheBallImageView];
    [self addSubview: self.loadingImageView];
    [self.loadingImageView setFrame: CGRectMake(self.topShowView.frame.origin.x + (self.topShowView.frame.size.width - pullBallWidth)/ 2,
                                                self.topShowView.frame.origin.y, pullBallWidth, pullBallHeight)];
    self.topShowView.loadingImageView = self.loadingImageView;
}

- (void)beginRefreshing {
    [self.topShowView beginLoading];
}

- (void)endRefreshing {
    self.topShowView.topRefreshStatus = HXSPullRefreshHeaderStateCanFinish;
}

- (void)removeRefreshHeader {
    if (self.topShowView) {
        [self.topShowView removeFromSuperview];
        self.topShowView = nil;
    }
    if (self.loadingImageView) {
        [self.loadingImageView removeFromSuperview];
        self.loadingImageView = nil;
    }
}

- (void)setLoadMoreProgressImageName: (NSString * )progressImageName
                    LoadingImageName: (NSString * )loadingImageName {
    self.progressImageName = progressImageName;
    self.loadingImageName = loadingImageName;
}

// MARK: - GET && SET

- (void)setObserver: (id)observer {
    objc_setAssociatedObject(self, @"observer", observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setTopShowView: (HXSPullRefreshImageView *)topShowView {
    [self willChangeValueForKey:@"HXSRefreshHeaderViewKey"];
    objc_setAssociatedObject(self, & HXSRefreshHeaderViewKey, topShowView, OBJC_ASSOCIATION_ASSIGN);
    [self didChangeValueForKey:@"HXSRefreshHeaderViewKey"];
}

- (void)setLoadingImageView: (FLAnimatedImageView *)loadingImageView {
    objc_setAssociatedObject(self, @"loadingImageView", loadingImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setProgressImageName: (NSString *)progressImageName {
    objc_setAssociatedObject(self, @"progressImageName", progressImageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)progressImageName {
    return objc_getAssociatedObject(self, @"progressImageName");
}

- (void)setLoadingImageName: (NSString *)loadingImageName {
    objc_setAssociatedObject(self, @"loadingImageName", loadingImageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)loadingImageName {
    return objc_getAssociatedObject(self, @"loadingImageName");
}

- (CGFloat)screenWidth {
    return CGRectGetWidth([[UIScreen mainScreen] bounds]);
}

- (id)observer {
    return objc_getAssociatedObject(self, @"observer");
}

- (FLAnimatedImageView *)loadingImageView {
    return objc_getAssociatedObject(self, @"loadingImageView");
}

- (HXSPullRefreshImageView *)topShowView {
    return objc_getAssociatedObject(self, & HXSRefreshHeaderViewKey);
}

- (FLAnimatedImageView *)createTheBallImageView {
    FLAnimatedImageView *loadIngImageView = [
                                             [FLAnimatedImageView alloc] init
                                             ];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *bundlePath = [bundle pathForResource:@"SVTableView"
                                            ofType:@"bundle"
                            ];
    if (bundlePath){
        bundle = [NSBundle bundleWithPath: bundlePath];
    }
    NSString *gifPath = [bundle pathForResource:@"59"
                                         ofType:@"gif"
                         ];
    NSData *data = [NSData dataWithContentsOfFile: gifPath];
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData: data];
    loadIngImageView.animatedImage = image;
    [loadIngImageView setHidden: YES];
    return loadIngImageView;
}

@end
